package main.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyEvent;
import main.dao.BookTableDB;
import main.model.Book;
import main.view.Main;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MangeBook {
    private String value;

    @FXML
    private TableView<Book> bookTable;

    @FXML
    private TableColumn<Book,String> bookName;

    @FXML
    private TableColumn<Book,String> bookPrice;

    @FXML
    private TableColumn<Book,String> bookAuthor;

    @FXML
    private TableColumn<Book,String> bookType;

    @FXML
    private TableColumn<Book,String> bookAmount;

    @FXML
    private TableColumn<Book,String> bookMessage;

    @FXML
    private TableColumn operator;

    @FXML
    private TextField searchbar;

    @FXML
    private Button search;
    @FXML
    private Button setbacks;

    @FXML
    void setbacks(ActionEvent event) throws IOException {
        new Main().initAdminMenu();

    }

    @FXML
    void inputConditions(KeyEvent event) {

    }

    @FXML
    void submitSearch(ActionEvent event) throws SQLException {
        this.value = this.searchbar.getText();
        bookName.setCellValueFactory(new PropertyValueFactory<>("title"));
        bookPrice.setCellValueFactory(new PropertyValueFactory<>("price"));
        bookAuthor.setCellValueFactory(new PropertyValueFactory<>("author"));
        bookType.setCellValueFactory(new PropertyValueFactory<>("type"));
        bookAmount.setCellValueFactory(new PropertyValueFactory<>("amount"));
        bookMessage.setCellValueFactory(new PropertyValueFactory<>("message"));
        ResultSet rst =  new BookTableDB().date(this.value);
        ObservableList<Book> data = FXCollections.observableArrayList();
        while(rst.next()) {
            Button edit = new Button("操作");
            data.add(new Book(rst.getString(1),rst.getString(2),rst.getString(3),rst.getString(4),rst.getString(5),rst.getString(6)));
            bookTable.setItems(data);
        }
    }
}
